package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.DatInfo;
import com.ruoyi.system.domain.Device;
import com.ruoyi.system.domain.Warn;
import com.ruoyi.system.mapper.DeviceMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.WarnMapper;
import com.ruoyi.system.service.IWarnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 报警Service业务层处理
 *
 * @author wxd
 * @date 2024-08-31
 */
@Service
public class WarnServiceImpl implements IWarnService {
    @Autowired
    private WarnMapper warnMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 查询报警
     *
     * @param id 报警主键
     * @return 报警
     */
    @Override
    public Warn selectWarnById(Long id) {
        return warnMapper.selectWarnById(id);
    }

    /**
     * 查询报警列表
     *
     * @param warn 报警
     * @return 报警
     */
    @Override
    public List<Warn> selectWarnList(Warn warn) {
        List<Warn> infos = warnMapper.selectWarnList(warn);
        List<String> deIds = infos.stream().map(Warn::getDeviceId).collect(Collectors.toList());
        /*if (CollectionUtil.isNotEmpty(deIds)) {
            List<Device> devices = deviceMapper.selectDeviceByIds(deIds.toArray(new String[0]));
            if (CollectionUtil.isNotEmpty(devices)) {
                Map<String, Device> map = devices.stream().collect(Collectors.toMap(Device::getId, p -> p));
                infos.forEach(x -> {
                    if (StrUtil.isNotEmpty(x.getDeviceId())) {
                        Device device = map.get(x.getDeviceId());
                        if (device != null) {
                            x.setDeviceModel(device.getModel());
                            x.setDeviceName(device.getName());
                        }
                    }
                });
                List<Long> deptIds = devices.stream().map(Device::getDeptId).collect(Collectors.toList());
                if (CollUtil.isNotEmpty(deptIds)) {
                    List<SysDept> depts = deptMapper.listByIds(deptIds.toArray(new Long[0]));
                    Map<Long, SysDept> deptMap = depts.stream().collect(Collectors.toMap(SysDept::getDeptId, p -> p));
                    infos.forEach(x -> {
                        SysDept sysDept = deptMap.get(x.getDeptId());
                        if (sysDept != null) {
                            x.setDeptName(sysDept.getDeptName());
                            x.setDeptStr(sysDept.getDeptName() + "(" + sysDept.getAddress() + ")");
                        }
                    });
                }
            }
        }*/

        return infos;
    }

    /**
     * 新增报警
     *
     * @param warn 报警
     * @return 结果
     */
    @Override
    public int insertWarn(Warn warn) {
        warn.setCreateTime(DateUtils.getNowDate());
        return warnMapper.insertWarn(warn);
    }

    /**
     * 修改报警
     *
     * @param warn 报警
     * @return 结果
     */
    @Override
    public int updateWarn(Warn warn) {
        warn.setUpdateTime(DateUtils.getNowDate());
        return warnMapper.updateWarn(warn);
    }

    /**
     * 批量删除报警
     *
     * @param ids 需要删除的报警主键
     * @return 结果
     */
    @Override
    public int deleteWarnByIds(Long[] ids) {
        return warnMapper.deleteWarnByIds(ids);
    }

    /**
     * 删除报警信息
     *
     * @param id 报警主键
     * @return 结果
     */
    @Override
    public int deleteWarnById(Long id) {
        return warnMapper.deleteWarnById(id);
    }

    @Override
    public void clear() {
        warnMapper.clear();
    }
}
